package com.hengtiansoft.lfy.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hengtiansoft.lfy.R;
import com.hengtiansoft.lfy.constants.Constants;
import com.hengtiansoft.lfy.db.EvaluatorDBManager;
import com.hengtiansoft.lfy.entity.EvaluatorMessageEntity;
import com.hengtiansoft.lfy.entity.EvaluatorModeEntity;
import com.hengtiansoft.lfy.entity.EvaluatorRankings;
import com.hengtiansoft.lfy.net.NetHelper;
import com.hengtiansoft.lfy.net.NetUtils;
import com.hengtiansoft.lfy.utils.EvaParser;
import com.hengtiansoft.lfy.utils.SharedUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: VoiceDialogActivity
 * @Description: 语音识别dialog
 * 
 */
public class TodayScoreDialogActivity extends Activity {
    private final static String TAG = TodayScoreDialogActivity.class.getSimpleName();

    private TextView tv_score;
    private TextView defeatednational;
    private ImageView iv_star1;
    private ImageView iv_star2;
    private ImageView iv_star3;
    private TextView tv_show;
    private RelativeLayout layout_close;
    private int score = 0;
    private int ranking = 0;

    //每个MODE下的详细信息
    private List<EvaluatorMessageEntity> evList ;
    //每个mode的信息
    private List<EvaluatorModeEntity> evModeList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_score);
        score = getIntent().getIntExtra("score",0);
        Bundle bundle = getIntent().getBundleExtra("bundle");
        if(bundle != null){
            ArrayList<EvaluatorMessageEntity> list = (ArrayList<EvaluatorMessageEntity>) bundle.getSerializable("list");
            evList = list;
        }
        tv_score = (TextView) findViewById(R.id.tv_allscore);
        tv_score.setText(""+score);
        defeatednational = (TextView) findViewById(R.id.tv_defeatednational);
        tv_show = (TextView) findViewById(R.id.tv_showscore);
        iv_star1 = (ImageView) findViewById(R.id.star1);
        iv_star2 = (ImageView) findViewById(R.id.star2);
        iv_star3 = (ImageView) findViewById(R.id.star3);
        layout_close = (RelativeLayout) findViewById(R.id.layout_close);

        layout_close.setOnClickListener(listener);
        tv_show.setOnClickListener(listener);
        if(evList == null || evList.size() <= 0)
            evModeList = EvaluatorDBManager.getInstance().getEvaluatorMode();
        if(evModeList != null && evModeList.size() > 0 ) {
            evList = EvaluatorDBManager.getInstance().getEvaluatorMessage(evModeList.get(0).getSerial());
        }
        String json = EvaParser.getInstance().getScoreJson(evList);
        NetHelper.uploadExerciseFinished(this,evList.get(0).getSerial(),score+"",json,new NetUtils.NetCallBack<EvaluatorRankings>(){
            @Override
            public void success(EvaluatorRankings rspData) {
                ranking = Math.round(Float.parseFloat(rspData.getRanking()) * 100);
                defeatednational.setText(ranking + getString(R.string.rankings));
                if(score > 95){
                    iv_star1.setImageResource(R.drawable.icon_star_selected);
                    iv_star2.setImageResource(R.drawable.icon_star_selected);
                    iv_star3.setImageResource(R.drawable.icon_star_selected);
                }else if(score > 66){
                    iv_star1.setImageResource(R.drawable.icon_star_selected);
                    iv_star2.setImageResource(R.drawable.icon_star_selected);
                    iv_star3.setImageResource(R.drawable.icon_star_normal);
                }else if(score > 33){
                    iv_star1.setImageResource(R.drawable.icon_star_selected);
                    iv_star2.setImageResource(R.drawable.icon_star_normal);
                    iv_star3.setImageResource(R.drawable.icon_star_normal);
                }else{
                    iv_star1.setImageResource(R.drawable.icon_star_normal);
                    iv_star2.setImageResource(R.drawable.icon_star_normal);
                    iv_star3.setImageResource(R.drawable.icon_star_normal);
                }
            }

            @Override
            public void failed(String msg) {
                Log.d("tag","failed = "+msg);
            }
        });
    }
    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.tv_showscore:
                    String share_body = getString(R.string.todayscore)+score +getString(R.string.score1)+" "+getString(R.string.defeatednational) + ranking + getString(R.string.rankings);
                    SharedUtil.shared(TodayScoreDialogActivity.this, getResources().getString(R.string.app_name), share_body, Constants.SHARE_URL);
                    break;
                case R.id.layout_close:
                    TodayScoreDialogActivity.this.finish();
                    break;
            }
        }
    };
}
